#!/bin/bash

# lumina AI 快速部署脚本
# 适用于腾讯云服务器

set -e  # 遇到错误立即退出

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_message() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_step() {
    echo -e "${BLUE}[STEP]${NC} $1"
}

# 检查是否为root用户
check_root() {
    if [ "$EUID" -ne 0 ]; then
        print_error "请使用 root 用户运行此脚本"
        exit 1
    fi
}

# 更新系统
update_system() {
    print_step "更新系统包..."
    apt update -y
    apt upgrade -y
}
update_system_yum() {
    print_step "更新系统包..."
    sudo yum makecache
    # 使用 --skip-broken 跳过有冲突的包，避免部署失败
    sudo yum update -y --skip-broken || true
}

# 安装必要软件
install_software() {
    print_step "安装必要软件..."
    apt install -y nginx git curl unzip wget
}
install_software_yum() {
    print_step "安装必要软件..."
   # sudo yum install -y epel-release        
    sudo yum install -y nginx git curl unzip wget
}

# 创建网站目录
create_web_directory() {
    print_step "创建网站目录..."
    mkdir -p /var/www/lumina-ai
    mkdir -p /etc/nginx/sites-enabled
#    mkdir -p /etc/nginx/sites-available/lumina-ai
    chown -R root:root /var/www/lumina-ai
    chown -R root:root /etc/nginx/sites-enabled
 #   chown -R root:root /etc/nginx/sites-available/lumina-ai
    chmod -R 755 /var/www/lumina-ai
     chmod -R 755 /etc/nginx/sites-enabled
  #  chmod -R 755  /etc/nginx/sites-available/lumina-ai
}

# 配置Nginx
configure_nginx() {
    print_step "配置Nginx..."
    
    # 备份原配置
    if [ -f /etc/nginx/sites-available/default ]; then
        mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.backup
    fi
    
    # 创建新的Nginx配置
    cat > /etc/nginx/sites-available/lumina-ai << 'EOF'
server {
    listen 80;
    server_name _;
    root /var/www/lumina-ai;
    index index.html;

    # 启用gzip压缩
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types
        text/plain
        text/css
        text/xml
        text/javascript
        application/json
        application/javascript
        application/xml+rss
        application/atom+xml
        image/svg+xml;

    # 静态资源缓存
    location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    # HTML文件缓存
    location ~* \.html$ {
        expires 1h;
        add_header Cache-Control "public, must-revalidate";
    }

    # 处理单页应用路由
    location / {
        try_files $uri $uri/ /index.html;
    }

    # 安全头
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;

    # 错误页面
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
}
EOF

    # 启用站点
    ln -sf /etc/nginx/sites-available/lumina-ai /etc/nginx/sites-enabled/
    
    # 测试配置
    if nginx -t; then
        print_message "Nginx配置测试通过"
        systemctl restart nginx
        systemctl enable nginx
        print_message "Nginx已启动并设置为开机自启"
    else
        print_error "Nginx配置测试失败"
        exit 1
    fi
}

# 配置防火墙
configure_firewall() {
    print_step "配置防火墙..."
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --permanent --add-port=22/tcp
   # ufw allow 80/tcp
    #ufw allow 443/tcp
   # ufw allow 22/tcp
   # ufw --force enable
   sudo firewall-cmd --reload
    print_message "防火墙已配置"
}

# 复制项目文件
copy_project_files() {
    print_step "复制项目文件..."
    
    # 检查当前目录是否有项目文件
    if [ -f "index.html" ]; then
        print_message "检测到项目目录，复制文件到网站目录..."
        cp -r * /var/www/lumina-ai/
        chown -R root:root /var/www/lumina-ai
        chmod -R 755 /var/www/lumina-ai
        print_message "项目文件已复制"
    else
        print_warning "未检测到项目文件，请手动复制项目文件到 /var/www/lumina-ai/ 目录"
    fi
}

# 显示部署结果
display_result() {
    print_message "部署完成！"
    echo ""
    echo "🌐 网站信息："
    echo "   - 网站地址: http://$(curl -s ifconfig.me)"
    echo "   - 网站目录: /var/www/lumina-ai/"
    echo ""
    echo "📝 后续操作："
    echo "   1. 将项目文件上传到 /var/www/lumina-ai/ 目录"
    echo "   2. 如果有域名，修改 Nginx 配置中的 server_name"
    echo "   3. 重启 Nginx: systemctl restart nginx"
    echo ""
    echo "🔧 常用命令："
    echo "   - 查看Nginx状态: systemctl status nginx"
    echo "   - 重启Nginx: systemctl restart nginx"
    echo "   - 查看错误日志: tail -f /var/log/nginx/error.log"
    echo "   - 查看访问日志: tail -f /var/log/nginx/access.log"
    echo ""
}

# 主函数
main() {
    echo "🚀 lumina AI 项目快速部署脚本"
    echo "=================================="
    
    check_root
    # 注释掉更新步骤，避免包冲突导致部署失败
    # update_system_yum
    install_software_yum
    create_web_directory
    configure_nginx
    configure_firewall
    copy_project_files
    display_result
}

# 运行 checklist
checklist() {
    echo ""
    echo "✅ 部署检查清单："
    echo "   □ Nginx 服务运行正常"
    echo "   □ 网站文件已上传"
    echo "   □ 防火墙已配置"
    echo "   □ 网站可以正常访问"
    echo ""
    echo "🔍 检查命令："
    echo "   systemctl status nginx"
    echo "   ls -la /var/www/lumina-ai/"
    echo "   curl -I http://localhost"
}

# 运行主函数
main
checklist 
